import { Injectable, EventEmitter } from '@angular/core';

@Injectable()
export class DrawSelectedStoreService {

  private store: any[] = [];
  private add$: EventEmitter<any[]> = new EventEmitter();
  private remove$: EventEmitter<any[]> = new EventEmitter();
  private change$: EventEmitter<any[]> = new EventEmitter();

  constructor() { }

  add(selected: any|any[]): void {
    let features: any[] = [];
    if(Array.isArray(selected)) {
      features = selected;
    } else {
      features = [selected];
    }
    this.store = [ ...this.store, ...features ];
    this.add$.emit(features);
    this.change$.emit(this.store);
  }

  remove(deselected: any|any[]): void {
    let features: any[] = [];
    if(Array.isArray(deselected)) {
      features = deselected;
    } else {
      features = [deselected];
    }
    this.store = this.store.filter(feature => !features.includes(feature));
    this.remove$.emit(deselected);
    this.change$.emit(this.store);    
  }

  selected(): any[] {
    return this.store;
  }

  on(event: 'add'|'remove'|'change'): EventEmitter<any[]> {
    switch(event) {
      case 'add': {
        return this.add$;
      }
      case 'remove': {
        return this.remove$;
      }
      case 'change': {
        return this.change$;
      }
      default: {
        return new EventEmitter<any[]>();
      }
    }
  }


}
